﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class AnalysisReport_AreaAnalysis : Frame.Pages.BasePage
{
    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        Frame.Global.GlobalFunctions.SetGridView(this.gridView, false);
        if (!IsPostBack && !IsCallback)
        {
            Dictionary<string, SqlParameter> ParamDic = new Dictionary<string, SqlParameter>();
            datStart.Date = DateTime.Today;
            datEnd.Date = DateTime.Today;
            ParamDic.Add("BeginDate", new SqlParameter("@BeginDate", DateTime.MinValue.AddYears(1900)));
            ParamDic.Add("EndDate", new SqlParameter("@EndDate", DateTime.MaxValue));

            using (DAL.CMSDALDataContext DContext = new DAL.CMSDALDataContext())
            {
                DAL.BI_Area[] Areas = DContext.BI_Area.ToArray();
                int i = 0;
                foreach (DAL.BI_Area item in Areas)
                {
                    if (i == 0)
                    {
                        ParamDic.Add("AreaID", new SqlParameter("@AreaID", item.AreaID));
                    }
                    TabArea.Tabs.Add(new DevExpress.Web.ASPxTabControl.Tab()
                    {
                        Index = item.AreaID,
                        VisibleIndex = i,
                        Text = item.AreaName,
                        ToolTip = string.Format("{0}\r\n{1}", item.AreaName, item.Remark),
                        NavigateUrl = string.Format("javascript:grid.PerformCallback('{0}')", item.AreaID)
                    });
                    i++;
                }
            }
            Session["TablesqlParams"] = ParamDic;
        }
        if (IsCallback)
        {
            if (this.datStart.Date == null)
            {
                this.datStart.Date = DateTime.Today;
            }
            if (this.datEnd.Date == null)
            {
                this.datEnd.Date = DateTime.Today;
            }
        }
        DDataBind();
    }


    DataTable table;
    public DataTable Table
    {
        get
        {
            if (Session["TablesqlParams"] != null)
            {
                table = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable(SqlStrFormat,
                    (Session["TablesqlParams"] as Dictionary<string, SqlParameter>).Values.ToArray());
            }
            return table;
        }
    }

    void DDataBind()
    {
        if (Session["TablesqlParams"] != null)
        {
            gridView.DataSource = Table;
            gridView.DataBind();
        }
    }

    const string SqlStrFormat = @"select b.TableName,
	                                     b.PCount, 
                                         COUNT(a.Table_ID) as Total 
                                  from   dbo.BLI_ConsumerHisDetail a ,
                                         dbo.BI_Tables b
                                  where  a.Table_ID=b.TableID and
                                         b.Area_ID=@AreaID and
                                         a.BookNo in (select BookNo 
                                                      from   dbo.BLI_ConsumerHis 
                                                      where  ConsumerDate >=@BeginDate and
                                                             ConsumerDate <@EndDate)      
                                  group by a.Table_ID,
                                           b.PCount,
                                           b.TableName
                                  order by Total desc";

    protected void QueryCBPanel_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
    {
        if (datStart.Date.Subtract(datEnd.Date).TotalDays > 0)
        {
            throw new Exception("结束时间必须大于开始时间！");
        }
        Dictionary<string, SqlParameter> ParamDic = Session["TablesqlParams"] as Dictionary<string, SqlParameter>;
        ParamDic["BeginDate"] = new SqlParameter("@BeginDate", datStart.Date);
        ParamDic["EndDate"] = new SqlParameter("@EndDate", datEnd.Date.AddHours(23).AddMinutes(59).AddSeconds(59));
        DDataBind();
    }

    protected void gridView_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
    {
        Dictionary<string, SqlParameter> ParamDic = Session["TablesqlParams"] as Dictionary<string, SqlParameter>;
        ParamDic["AreaID"] = new SqlParameter("@AreaID", int.Parse(e.Parameters));
        DDataBind();
    }

    protected override string ActionID
    {
        get { return "0"; }
    }
}